#include <iostream>

using namespace std;

const int N = 5100;

int n, m, x, y, v;
int a[N][N];
int f[N][N];

int main()
{
    // n个目标，正方形边长为m
    cin >> n >> m;
    while(n--)
    {
        cin >> x >> y >> v;
        x++, y++; // 下标从1开始
        a[x][y] += v; //每个位置可能有多个目标
    }
    n = 5001;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            f[i][j] = f[i - 1][j] + f[i][j - 1] -f[i - 1][j - 1] + a[i][j];
        }
    }

    int ret = 0;
    m = min(m, n); // 如果m很大就可以把整个区域全部炸毁
    // 枚举所有边长为m的正方形
    for(int x2 = m; x2 <= n; x2++)
    {
        for(int y2 = m; y2 <= n; y2++)
        {
            int x1 = x2 - m + 1, y1 = y2 - m + 1;
            ret = max(ret, f[x2][y2] - f[x1 - 1][y2] - f[x2][y1 - 1] + f[x1 - 1][y1 - 1]);
        }
    }
    cout << ret << endl;
    return 0;
}